//
//  ProfileEditNameViewController.m
//  Cat
//
//  Created by lushitong on 2019/1/2.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "ProfileEditNameViewController.h"
#import "ProfileInputView.h"
#import "SetViewController.h"
#import "UserInfoManager.h"
#import "FMDBUser.h"

@interface ProfileEditNameViewController () <UITextFieldDelegate>
@property (nonatomic, strong) ProfileInputView *nameInputView;
@property (nonatomic, strong) UILabel *desLb;

@property (nonatomic, assign) BOOL didSetupConstraints;

@property (nonatomic, strong) NSArray *filterWords;

@end

@implementation ProfileEditNameViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	
	[self p_setTopTitleDetail:@{Nav_Title:@"昵称"}.mutableCopy];
	
	self.nameInputView.textField.text = self.name;
	[self.nameInputView.textField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
	
	NSString *content = [self.nameInputView.textField.text trimmingSartEndWhitespace];
	
	if (!content || [content isEqualToString:@""]) {
		return;
	}
}

- (void)loadView {
	[super loadView];
	
	[self.view addSubview:self.nameInputView];
	[self.view addSubview:self.desLb];
	
	[self.view setNeedsUpdateConstraints];
}

- (void)updateViewConstraints {
	[super updateViewConstraints];
	
	if (!self.didSetupConstraints) {
		[self.nameInputView autoPinToTopLayoutGuideOfViewController:self withInset:NavigationBarHeight];
		[self.nameInputView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
		[self.nameInputView autoPinEdgeToSuperviewEdge:ALEdgeRight];
		[self.nameInputView autoSetDimension:ALDimensionHeight toSize:FloatWith3x(170.0)];
		
		[self.desLb autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.nameInputView withOffset:10.0];
		[self.desLb autoAlignAxisToSuperviewAxis:ALAxisVertical];
		[self.desLb autoSetDimension:ALDimensionWidth toSize:(ScreenWidth - 20) relation:NSLayoutRelationLessThanOrEqual];
		
		self.didSetupConstraints = YES;
	}
}

#pragma mark - Pri Method

- (void)inputViewValueChanged:(UITextField *)textField {
	
	if (textField.markedTextRange) {
		return;
	}
	
	if (textField.text.length > 11) {
		[FLProgressHUD showMessageCenter:@"请输入11字以内"];
		 NSString *text = textField.text;
		int currentLength = (int)text.length;
		if (currentLength > 11) {
			textField.text = [text substringToIndex:11];
		}
		return;
	}
	textField.textColor = [UIColor blackColor];
}

- (void)p_topLeftBtnClick {
	
	[self.view endEditing:YES];
	
	NSMutableAttributedString *attString = nil;
	
	NSString *content = [self.nameInputView.textField.text trimmingSartEndWhitespace];
	
	NSRange range;
	for (NSString *word in self.filterWords) {
		range = [content rangeOfString:word];
		if (range.length > 0) {
			if (!attString) {
				attString = [[NSMutableAttributedString alloc] initWithString:content];
			}
			
			[attString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
		}
	}
	
	if (attString) {
		self.desLb.textColor = [UIColor redColor];
		self.desLb.text = @"昵称含有敏感内容";
		self.nameInputView.textField.attributedText = attString;
	} else {
		FMDBUser *user = [UserInfoManager userWithPhone:UDPhone];
		user.nickName = content;
		[user bg_saveOrUpdate];
		[self.navigationController popViewControllerAnimated:YES];
	}
	
}

#pragma mark - Pri Property

- (ProfileInputView *)nameInputView {
	if (!_nameInputView) {
		_nameInputView = [[ProfileInputView alloc] init];
		_nameInputView.textField.placeholder = @"昵称";
		[_nameInputView.textField addTarget:self action:@selector(inputViewValueChanged:) forControlEvents:UIControlEventEditingChanged];
	}
	return _nameInputView;
}

- (UILabel *)desLb {
	if (!_desLb) {
		_desLb = [[UILabel alloc] init];
		_desLb.font = [UIFont fontWith3x:42.0];
		_desLb.textColor = COLOR_WITH_HEX(0x999999);
		_desLb.numberOfLines = 0;
		_desLb.textAlignment = NSTextAlignmentCenter;
	}
	return _desLb;
}


@end
